<?php

class DkpAdjustmentRow extends Zend_Db_Table_Row_Abstract
{
	
	/**
	 * This function is called by the row class before a delete.
	 * 
	 * Here we make sure that characters will have their 
	 * dkp recalculated after a adjustment affecting them 
	 * has been deleted.
	 * 
	 */
	protected  function _delete()
	{
		$currentCharacterIds = $this->getCurrentCharactersAsIds();
		$this->queueRecalculation($this->time,$currentCharacterIds);
	}
	
	/**
	 * Clear all characters from this adjustment.
	 * 
	 * For internal use only.
	 *
	 */
	private function clearCharacters()
	{
		$dkpAdjustmentCharacters = $this->findDependentRowset("DkpAdjustmentsCharacters", "DkpAdjustments");
		
		foreach($dkpAdjustmentCharacters as $dkpAdjustmentCharacter) {
			$dkpAdjustmentCharacter->delete();
		}
	}
	
	/**
	 * Returns a array of character ids for this adjustment.
	 *
	 * @return array	array of characters ids associated with this adjustment
	 */
	public function getCurrentCharactersAsIds()
	{
		$result = array();
		
		// Find the charcters associated with the adjustment
		$dkpAdjustmentCharacters = $this->findDependentRowset("DkpAdjustmentsCharacters", "DkpAdjustments");
		
		// Add their ids to the result
		foreach($dkpAdjustmentCharacters as $character) {
			$result[] = $character->character_id;
		}
		
		return $result;
	}
	
	/**
	 * Set the characters for this adjustment.
	 * 
	 * Any change of characters compared to the previous ones
	 * will trigger a recalculation of the dkp.
	 * 
	 * @param array array of character ids
	 */
	public function setCharacters($characterIds)
	{
		// Get the characters currently affected by this adjustment
		$currentCharacterIds = array();
		$dkpAdjustmentCharacters = $this->findDependentRowset("DkpAdjustmentsCharacters", "DkpAdjustments");
		foreach($dkpAdjustmentCharacters as $character) {
			$currentCharacterIds[] = $character->character_id;
		}
		
		// The characters needing updates are those either removed or added
		$charactersNeedingUpdate = array_merge(array_diff($characterIds, $currentCharacterIds), 
		                                       array_diff($currentCharacterIds, $characterIds));
		$this->queueRecalculation($this->time, $charactersNeedingUpdate);

		// Remove all characters currently in this adjusment
		$this->clearCharacters();
		
		// Add all the new characters to the adjustment
		$dkpAdjustmentCharactersTable = new DkpAdjustmentsCharacters();
		foreach($characterIds as $characterId) {
			$data = array ("dkp_adjustment_id" => $this->id, "character_id" => $characterId );
			$dkpAdjustmentCharactersTable->insert($data);
		}
		
        /*
		echo "current:<br/>";
		var_dump($currentCharacterIds);
		
		echo "<br/>new:</<br>";
		var_dump($characterIds);
		echo "<br/>";
		
		echo "needing update: <br/>";
		var_dump($charactersNeedingUpdate);
        */
	}
	
	protected function queueRecalculation($time, $characterIds)
	{
		// Get the characters currently quede for for updating (DKP plugin guarantees that this will be an array)
		$currentCharactersIds = Zend_Registry::getInstance()->get("characters_needing_update");
		
		// Put all characters that need updates in the currentCharacters
		foreach($characterIds as $characterId) {
			// Skip any character that is already in the list and have a lower update time
			if (isset($currentCharactersIds[$characterId]) && $currentCharactersIds[$characterId] <= $time) {
				continue;
			}
			$currentCharactersIds[$characterId] = $time;
		}
		
		// Put the characters back into the array
		Zend_Registry::getInstance()->set("characters_needing_update", $currentCharactersIds);
	}
}